package com.tanhua.manage.listener;

import com.alibaba.fastjson.JSON;
import com.tanhua.manage.domain.Log;
import com.tanhua.manage.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
@Slf4j
@RocketMQMessageListener(topic = "tanhua-log",consumerGroup = "tanhua-log-consumer")
public class LogMessageListener implements RocketMQListener<String> {

    @Autowired
    private LogService logService;

    @Override
    public void onMessage(String message) {
        log.info("======读取rocketmq的消息插入日志表信息=======>");
        //1. 把消息转换为Map对象，取出里面数据
        Map<String,Object> map = JSON.parseObject(message, Map.class);
        String type = (String) map.get("type");
        Integer userId = (Integer) map.get("userId");
        String logTime = (String) map.get("log_time");
        //2. 创建Log对象补全数据，然后插入数据即可
        Log log = new Log();
        log.setUserId(userId.longValue()); //操作人的id
        log.setLogTime(logTime); //操作的时间
        log.setType(type); //操作的类型
        log.setCreated(new Date());
        log.setUpdated(new Date());
        logService.save(log);

    }
}
